#!/bin/bash
# filename: service_info.sh

echo -n "请输入服务名称: "
read service_name

# 检查服务是否运行
if systemctl is-active --quiet "$service_name"; then
    # 获取PID
    pid=$(systemctl show "$service_name" --property=MainPID | cut -d'=' -f2)
    
    # 获取端口（需要netstat或ss命令）
    port=$(ss -tlnp | grep "pid=$pid" | awk '{print $4}' | cut -d':' -f2 | head -1)
    
    # 获取启动时间
    start_time=$(ps -o lstart= -p "$pid" 2>/dev/null)
    
    echo "服务名称: $service_name"
    echo "PID: $pid"
    echo "端口: ${port:-未知}"
    echo "启动时间: ${start_time:-未知}"
else
    echo "服务 $service_name 未运行"
fi
